package org.oschina.api.report.dao;

import org.oschina.Constants;
import org.oschina.api.BaseDAO;
import org.oschina.api.report.model.Report;
import org.oschina.api.report.vo.ReportVo;
import org.springframework.stereotype.Repository;

@Repository
public class ReportDAO extends BaseDAO {

	private final String findVo="SELECT re.id,re.major,re.content,re.create_time,us.id as 'user.id',us.username as 'user.username' FROM "+Constants.DB_REPORT+" re,"+Constants.DB_USER+" us WHERE re.user=us.id";
	
	private final String findVoByMajorAndType=findVo+" AND re.major=? AND re.type=?";
	/**
	 * 根据主键和类型返回
	 * @param major
	 * @param type
	 * @return
	 */
	public ReportVo findVoByMajorAndType(int major,int type)
	{
		return (ReportVo) getQueryHelper().queryForObject(findVoByMajorAndType, new Object[]{major,type}, ReportVo.class);
	}
	
	/**
	 * 根据主键和类型删除
	 * @param major
	 * @param type
	 * @return
	 */
	public int deleteByMajorAndType(int major,int type)
	{
		return (int) getQueryHelper().delete(new String[]{"major=?","type=?"}, new Object[]{major,type}, ReportVo.class);
	}
	
	/**
	 * 创建
	 * @param report
	 * @return
	 */
	public Report create(Report report)
	{
		getQueryHelper().insert(new String[]{"id","major","user","type","content","create_time"}, 
				new Object[]{report.getId(),report.getMajor(),report.getUser(),report.getType(),report.getContent(),report.getCreate_time()}, ReportVo.class);
		return report;
	}
}